package com.droidnova.android.games.vortex;

import java.io.BufferedReader;

import android.content.Context;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.microedition.khronos.opengles.GL10;



public class points {

	public static final String LOG_TAG = "Droidnova";

	private int _noVertices = 0;
	private float[] _pvertices ,_intensity ;
	private String _new[] ,_even[],_odd[] , _points[];
	float max_value=0.0f;
	float cr = 0.9f,cg=0.3f ,ca=1.0f;
	int number;
	float _pcolors[]; 
	//calculate color values for each point
	int count=0;
	float i_value=0.0f,cb = 0.0f;
	// Our vertex buffer.
	private FloatBuffer _pvertexBuffer;
	//our Color buffer
	private FloatBuffer _pcolorBuffer;

	public points(Context ctx)
	{


		Log.w("MyClassName", "points");


		InputStream fIn = ctx.getResources().openRawResource(R.raw.input1);
		if(fIn==null)
		{
			Log.w("Nullerror", "null");
		}
		else
		{

			InputStreamReader isr = new InputStreamReader(fIn);
			BufferedReader br = new BufferedReader(isr);
			String stringRead = null;

			ArrayList <String> words = new ArrayList<String> ();
			ArrayList <String> points = new ArrayList<String>();

			ArrayList<String> even = new ArrayList<String>();
			ArrayList<String> odd = new ArrayList<String>();



			try {
				stringRead = br.readLine( );
			} catch (IOException e) {

				e.printStackTrace();
			}
			while( stringRead != null ) // end of the file?
			{

				StringTokenizer st = new StringTokenizer(stringRead,",");

				while(st.hasMoreTokens())
				{
					words.add(st.nextToken());

				}

				try {
					stringRead = br.readLine( );
				} catch (IOException e) {

					e.printStackTrace();
				}
				_new = words.toArray(new String[words.size()]);

			}


			for(int i=0;i<_new.length;i+=2)
			{
				even.add(_new[i]);	
			}

			for(int i=1;i<_new.length;i+=2)
			{
				odd.add(_new[i]);	
			}
			_even = even.toArray(new String[even.size()]);
			_odd = odd.toArray(new String[odd.size()]);


			for(int i=0 ;i < _even.length;i++)
			{
				StringTokenizer st = new StringTokenizer(_even[i], " "); 
				while(st.hasMoreTokens())
				{
					points.add(st.nextToken()+ "f");

				}
			}
			_points = points.toArray(new String[points.size()]);
			try{
				_pvertices = convertStringArraytoFloatArray(_points);
				_intensity = convertStringArraytoFloatArray(_odd);
			} catch (Exception e) {

				e.printStackTrace();

			}


		}



		_noVertices = _intensity.length ;
		 number = _intensity.length *4;
		_pcolors = new float[number];
		max_value = getMaxValue(_intensity);
		for(int i = 0;i<_intensity.length;i++)
		{		
			i_value = _intensity[i];
			cb = i_value/max_value;
			for(int j=0;j<=3;j++)
			{
				if(j==0)
				{
					_pcolors[count]=cr;	
				}
				else if(j==1)
				{
					_pcolors[count]=cg;	
				}
				else if(j==2)
				{
					_pcolors[count]=cb;
				}
				else
				{
					_pcolors[count]=ca;	
				}
				count++;
			}

		}

		ByteBuffer vbb = ByteBuffer.allocateDirect(_pvertices.length * 4);
		vbb.order(ByteOrder.nativeOrder());
		_pvertexBuffer = vbb.asFloatBuffer();
		_pvertexBuffer.put(_pvertices);
		_pvertexBuffer.position(0);	

		//color buffer
		ByteBuffer cbb = ByteBuffer.allocateDirect(_pcolors.length * 4);
		cbb.order(ByteOrder.nativeOrder());
		_pcolorBuffer = cbb.asFloatBuffer();
		_pcolorBuffer.put(_pcolors);
		_pcolorBuffer.position(0);


	}               //END OF POINTS CONTRUCTOR >>>>>>>>



	//conversion ofstring array to float array	
	public float[] convertStringArraytoFloatArray(String[] _new) throws Exception 
	{
		if (_new != null)
		{
			float floatarray[] = new float[_new.length];
			for (int i = 0; i < _new.length; i++) {
				floatarray[i] = Float.parseFloat(_new[i]);
			}
			return floatarray;
		}
		return null;
	}
	// Finding the maximum value in an array
	public float getMaxValue(float[] numbers){  
		float maxValue = numbers[0];  
		for(int i=1;i < numbers.length;i++){  
			if(numbers[i] > maxValue){  
				maxValue = numbers[i];
			}  
		}  
		return maxValue;  
	}  

	public void draw(GL10 gl) {

		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
	
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, _pvertexBuffer);
		gl.glColorPointer(4, GL10.GL_FLOAT, 0, _pcolorBuffer);
		//set the point size
		gl.glPointSize(8);
		// finally draw the vertices
		gl.glRotatef(45.0f, 0.0f,0.0f,1.0f);
		gl.glDrawArrays(GL10.GL_POINTS, 0, _noVertices);
	
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
	}
}
